﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class PriceCust : System.Web.UI.Page
    {
        protected int PolicyId = 0;
        private static ICustomerBLL customerBLL = IocContainer.Instance.Resolve<ICustomerBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["policyId"], out PolicyId);
            if (!IsPostBack)
            {
                IPricePolicyBLL policyBll = IocContainer.Instance.Resolve<IPricePolicyBLL>();
                PricePolicy policy = policyBll.GetPolicyById(PolicyId);
                if (policy != null && !string.IsNullOrEmpty(policy.PolicyName))
                {
                    this.txtPolicyName.Text ="【"+policy.PolicyName + "】";
                }
            }
        }

      
        [WebMethod]
       public static Boolean DeleteCust(int custId,int policyId)
       {
           
            Boolean result = false;
            try
            {
                customerBLL.DeletePriceCust(policyId,custId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
       }

        [WebMethod]
        public static string GetPolicyCusts(string name, string code, string policyId, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(name))
            {
                strWhere.Append(" and FName like '%" + name + "%'");
            }
            if (!string.IsNullOrEmpty(code))
            {
                strWhere.Append(" and FNumber like '%" + code + "%'");
            }
            if (!string.IsNullOrEmpty(policyId))
            {
                strWhere.Append(" and exists(select * from T_Sales_PriceCust as tsp where tsp.PolicyId ="+policyId
                + " and tsp.CustId = T_BasicData_Customer.FItemId )" );
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  asc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = customerBLL.GetCustomerssByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";

        }



    }
}
